package hongniang

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shangyuapi/data_source"
	"shangyuapi/models"
	"shangyuapi/utils"
	"strings"
)

func Hongnianginfo(ctx *gin.Context) {
	request := UpdatezlBody{}
	ctx.ShouldBind(&request)
	hongniangid := request.Hongniangid
	var hongniang models.Hongniang
	data_source.Db.Where("id = ?", hongniangid).First(&hongniang)
	var orders []models.Orders
	data_source.Db.Select("id, fwprice").Where("hongniangid = ?", hongniangid).Where("status = 1 or status = 3 or status = 4").Find(&orders)
	var drzprice float64
	for _, o := range orders {
		drzprice = drzprice + o.Fwprice
	}
	hongniang.Drzprice = drzprice
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    hongniang,
		"msg":     "ok",
	})
	return
}

func Hongniangfuwu(ctx *gin.Context) {
	request := UpdatezlBody{}
	ctx.ShouldBind(&request)
	hongniangid := request.Hongniangid
	var fuwus []models.Fuwu
	data_source.Db.Find(&fuwus)

	var hongniangfuwus []models.Hongniangfuwu
	data_source.Db.Where("hongniangid = ?", hongniangid).Find(&hongniangfuwus)

	for k, v := range fuwus {
		fuwus[k].Xuanzhong = 1
		for _, h := range hongniangfuwus {
			if v.ID == h.Fuwuid {
				fuwus[k].Xuanzhong = 2
			}
		}
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    fuwus,
		"msg":     "ok",
	})
	return
}

func Hongniangfuwuupdate(ctx *gin.Context) {
	request := UpdatezlBody{}
	ctx.ShouldBind(&request)
	hongniangid := request.Hongniangid
	fuwuids := request.Fuwuids

	fuwuidarr := strings.Split(fuwuids, ",")
	var hnfuwus []models.Hongniangfuwu
	for _, v := range fuwuidarr {
		hnfw := models.Hongniangfuwu{
			Hongniangid: hongniangid,
			Fuwuid:      utils.StrToInt64(v),
		}
		hnfuwus = append(hnfuwus, hnfw)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
